package com.ziyueyuwen.xiniao.controller.wx;

import com.alibaba.fastjson.JSONObject;
import com.suyeer.basic.util.HttpResUtil;
import com.suyeer.basic.util.LogUtil;
import com.suyeer.fastwechat.bean.fwpay.FwPrepayBean;
import com.suyeer.fastwechat.util.FwPayUtil;
import com.suyeer.fastwechat.util.FwUtil;
import com.ziyueyuwen.xiniao.base.HttpResContent;
import com.ziyueyuwen.xiniao.enums.HttpStatusEnum;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.Charset;

import static com.ziyueyuwen.xiniao.base.BaseUtil.saveErrorMsgAndData;
import static com.ziyueyuwen.xiniao.base.BaseUtil.saveSuccessMsgAndData;

/**
 * @author jun 2019/1/16
 */
@Controller
@RequestMapping("/wx/pay")
public class WxPayController {

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String index() {
        return "/wx/pay";
    }

    @RequestMapping(value = "/get_prepay_info_jsapi.do", method = RequestMethod.POST)
    public void getPrepayInfoJsApi(HttpServletRequest request, HttpServletResponse response) {
        HttpResContent<JSONObject> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取微信公众号支付参数");
        try {
            // do.
            String openId = hrc.getWxUserInfo().getOpenid();
            String userIp = hrc.getUserIp();
            String body = "子曰支付测试";
            String outTradeNoPreFix = "1000000";
            String fee = "1";
            FwPrepayBean wxPrepayBean = new FwPrepayBean();
            wxPrepayBean.setSpbill_create_ip(userIp);
            wxPrepayBean.setOpenid(openId);
            wxPrepayBean.setBody(body);
            wxPrepayBean.setTotal_fee(fee);
            wxPrepayBean.setTrade_type("JSAPI");
            wxPrepayBean.setNotify_url("https://wx.ziyueyuwen.com/wx/pay/notify.do");
            wxPrepayBean.setOut_trade_no(FwPayUtil.getOrderNumber(outTradeNoPreFix));
            JSONObject retObj = FwPayUtil.getPayParams(wxPrepayBean);
            saveSuccessMsgAndData(hrc, retObj);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    /**
     * h5支付回调
     *
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     */
    @RequestMapping(value = "/notify.do", method = RequestMethod.POST)
    public void notify(HttpServletRequest request, HttpServletResponse response) {
        HttpResContent<String> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("公众号支付结果通知");
        try {
            // 读取通知内容
            String notifyContent = IOUtils.toString(request.getInputStream(), Charset.defaultCharset());
            LogUtil.info("微信支付回调: {}", notifyContent);
            saveSuccessMsgAndData(hrc, FwUtil.getPaySuccessNoticeReturnXmlContent());
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, HttpStatusEnum.SERVER_ERROR.getCode(), FwUtil.getPayFailNoticeReturnXmlContent(), e);
        } finally {
            HttpResUtil.writeOnlyResult(hrc);
        }
    }

}
